package de.blinkt.openvpn.advancedsettings2;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import de.blinkt.openvpn.R;
import de.blinkt.openvpn.advancedsettings2.AdvancedSettingsPresenter.NetworkListItem;
import de.blinkt.openvpn.advancedsettings2.ConfigureNetworkItemPresenter.ConfigureNetworkItemFinishedCallback;

public class Settings_Advanced extends Fragment implements AdvancedSettingsView {
	private View mView;
	
	private CheckBox mRestrictToNetworks;
	private Button mAddCurrentButton;

	private ListView mNetworkList;

	private AdvancedSettingsPresenter mPresenter;

	public Settings_Advanced() {
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.advanced_settings,container,false);
		
		mRestrictToNetworks = (CheckBox) mView.findViewById(R.id.restrictToListedNetworksCheckbox);
		mAddCurrentButton = (Button)mView.findViewById(R.id.addCurrentNetworkButton);
		mNetworkList = (ListView)mView.findViewById(R.id.networkList);

		
		return mView;
	}

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		mPresenter = new AdvancedSettingsPresenter(this);
		if(savedInstanceState == null){
			mPresenter.loadModelFromStorage(getActivity(), getArguments().getString(getActivity().getPackageName() + ".profileUUID"));	
		} else {
			mPresenter.loadModelFromSavedState(getActivity(), savedInstanceState);	
		}
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mPresenter.saveModel(outState);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		mPresenter.populateViewFromProfile();
	}
	
	public void onPause() {
		super.onPause();
		mPresenter.saveViewToProfile();
	}
	
	@Override
	public CheckBox getRestrictToNetworks() {
		return mRestrictToNetworks;
	}
	
	@Override
	public Button getAddCurrentButton() {
		return mAddCurrentButton;
	}

	
	@Override
	public ListView getNetworkList() {
		return mNetworkList;
	}

	@Override
	public void showUnableToAddNetworkError() {
		showAlertDialog(R.string.unsupportedNetworkTypeErrorMessage, R.string.unsupportedNetworkTypeErrorTitle);
	}


	private void showAlertDialog(int errorMessageResourceId, int errorTitleResourceId) {
		AlertDialog create = new AlertDialog.Builder(getActivity()).
		setCancelable(false).
		setMessage(errorMessageResourceId).
		setTitle(errorTitleResourceId).
		setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		}).create();
		create.show();
	}


	@Override
	public ConfigureNetworkItemView showConfigureNetworkItemView() {
		ConfigureNetworkDialogFragment newFragment = new ConfigureNetworkDialogFragment();
		newFragment.setParentFragmentID(getId());
	    newFragment.show(getFragmentManager(), "dialog");
	    return newFragment;
	}


	@Override
	public void showNotConnectedToNetworkError() {
		showAlertDialog(R.string.notConnectedToNetworkErrorMessage, R.string.notConnectedToNetworkErrorTitle);
	}


	@Override
	public void showNetworkAlreadyExistsError() {
		showAlertDialog(R.string.nextworkExistsErrorMessage, R.string.nextworkExistsErrorTitle);
	}
	
	public ConfigureNetworkItemFinishedCallback getFinishednetworkItemCallback() {
		return mPresenter;
	}

	public NetworkListItem getCurrentlySelectedItem() {
		return mPresenter.getCurrentlySelectedItem();
	}
	
}
